package com.example.history.service.impl;



import com.alibaba.fastjson.JSON;
import com.example.common.dtos.ResponseResult;
import com.example.history.entity.History;
import com.example.history.mapper.HistoryMapper;
import com.example.history.service.IHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import static com.example.Redis自定义封装方法.RedisM.JavaInRedis;
import static com.example.Redis自定义封装方法.RedisM.RedisInJava;
import static com.example.utils.RedisConstants.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 凡哥哥
 * @since 2023-11-25
 */
@Service
@Transactional
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements IHistoryService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 添加历史记录,到Redis中
     *
     * @param history
     * @return
     */
    @Override
    public void historyAdd(History history) {
        if (!Objects.isNull(history)) {
            if (history.getUserId() != null) {
                //存储
                String historyKey = LOGIN_USER_HISTORY_KEY + history.getUserId().toString() + ":" + history
                        .getBookId().toString();

                if (stringRedisTemplate.keys(historyKey) != null) {
                    stringRedisTemplate.delete(historyKey);
                    //将java对象转化为JSON,以字符串的形式,存入Redis的set集合中
                    JavaInRedis(historyKey, history);
                    //设置history有效期
                    //stringRedisTemplate.expire(historyKey,LOGIN_USER_TTL, TimeUnit.MINUTES);
                    //永久有效
                    stringRedisTemplate.persist(historyKey);
                }
            } else {
                return;
            }
        }

    }

    /**
     * 在Redis中获取历史记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<History> historyList(Long userId) {
        String historyKey = LOGIN_USER_HISTORY_KEY + userId + ":" + "*";
        Set<String> keys = stringRedisTemplate.keys(historyKey);
        List<History> history = new ArrayList<>();
        for (String key : keys) {
            Set<String> members = stringRedisTemplate.opsForSet().members(key);
            for (String member : members) {
                //JSON字符串解析为Java对象
                History lishi = RedisInJava(member, History.class);
                history.add(lishi);
            }
        }
        return history;
    }

    /**
     * 删除历史记录
     *
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteHistorys(String ids,int userId) {
        int[] ints = JSON.parseObject(ids, int[].class);

        for (int id : ints) {
            String historyKey = LOGIN_USER_HISTORY_KEY + userId + ":" + id;
            stringRedisTemplate.delete(historyKey);
        }
        return ResponseResult.okResult("删除成功");
    }


}
